import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/common/theme/dimens.dart';
import 'package:flutter_taotaoshipin/util/theme_utils.dart';

/// 默认字号18，白字蓝底，高度48
class MyButton extends StatelessWidget {
  const MyButton(
      {super.key,
      this.text = '',
      this.fontSize = Dimens.font_sp18,
      this.textColor,
      this.disabledTextColor,
      this.backgroundColor,
      this.disabledBackgroundColor,
      this.minHeight = 48.0,
      this.minWidth = double.infinity,
      this.onPressed,
      this.padding = const EdgeInsets.symmetric(horizontal: 16.0),
      this.radius = 2.0,
      this.side = BorderSide.none});

  final String text;
  final double fontSize;
  final Color? textColor;
  final Color? disabledTextColor;
  final Color? backgroundColor;
  final Color? disabledBackgroundColor;
  final double? minHeight;
  final double? minWidth;
  final VoidCallback? onPressed;
  final EdgeInsetsGeometry padding;
  final double radius;
  final BorderSide side;

  @override
  Widget build(BuildContext context) {
    final bool isDark = context.isDark;
    return TextButton(
      onPressed: onPressed,
      style: ButtonStyle(
        // 文字颜色
        foregroundColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.disabled)) {
            return disabledTextColor ?? Colors.grey;
          }
          return textColor ?? Colors.white;
        }),
        // 背景颜色
        backgroundColor: MaterialStateProperty.resolveWith((states) {
          if (states.contains(MaterialState.disabled)) {
            return disabledBackgroundColor ?? Colors.grey;
          }
          return backgroundColor ?? AppColors.app_main;
        }),
        // 水波纹
        overlayColor: MaterialStateProperty.resolveWith((states) {
          return (textColor ?? Colors.white).withOpacity(0.12);
        }),
        // 按钮最小大小
        minimumSize: (minWidth == null || minHeight == null)
            ? null
            : MaterialStatePropertyAll(Size(minWidth!, minHeight!)),
        padding: MaterialStatePropertyAll(padding),
        shape: MaterialStatePropertyAll(RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(radius))),
        side: MaterialStatePropertyAll(side),
      ),
      child: Text(
        text,
        style: TextStyle(fontSize: fontSize),
      ),
    );
  }
}
